package com.yijiu.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yijiu.wiki.domain.Content;
import com.yijiu.wiki.domain.Doc;
import com.yijiu.wiki.domain.DocExample;
import com.yijiu.wiki.mapper.ContentMapper;
import com.yijiu.wiki.mapper.DocMapper;
import com.yijiu.wiki.req.DocQueryReq;
import com.yijiu.wiki.req.DocSaveReq;
import com.yijiu.wiki.resp.DocQueryResp;
import com.yijiu.wiki.resp.PageResp;
import com.yijiu.wiki.util.CopyUtil;
import com.yijiu.wiki.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ligaojian
 * @version 1.0
 * @description: TODO
 * @date 2021/10/30 9:47
 */
@Service
public class DocService {

    @Resource
    private DocMapper docMapper;

    @Resource
    private ContentMapper contentMapper;

    @Resource
    private SnowFlake snowFlake;

    public List<DocQueryResp> all(Long ebookId) {

        DocExample docExample = new DocExample();
        docExample.createCriteria().andEbookIdEqualTo(ebookId);
        docExample.setOrderByClause("sort asc");
        List<Doc> docList = docMapper.selectByExample(docExample);
        //列表复制
        List<DocQueryResp> list = CopyUtil.copyList(docList, DocQueryResp.class);

        return list;
    }
    public PageResp<DocQueryResp> list(DocQueryReq req){

        DocExample docExample = new DocExample();
        docExample.setOrderByClause("sort asc");
        DocExample.Criteria criteria = docExample.createCriteria();
        //使用pageHelper插件实现分页
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Doc> docList = docMapper.selectByExample(docExample);

        PageInfo<Doc> pageInfo = new PageInfo<>(docList);
//        pageInfo.getTotal();
//        pageInfo.getPages();
//        List<DocResp> respList = new ArrayList<>();
//        for (Doc doc : docList) {
////            DocResp docResp = new DocResp();
////            BeanUtils.copyProperties(doc,docResp); //对象的复制
//
//            DocResp docResp = CopyUtil.copy(doc, DocResp.class);
//            respList.add(docResp);
//        }

        List<DocQueryResp> list = CopyUtil.copyList(docList, DocQueryResp.class);
        PageResp<DocQueryResp> pageResp = new PageResp();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);

        return pageResp;
    }

    /**
    * @description: 编辑电子书中的保存
    * @author ligaojian
    * @date
    * @version 1.0
    */
    public void save(DocSaveReq req) {
        Doc doc = CopyUtil.copy(req,Doc.class);
        Content content = CopyUtil.copy(req, Content.class);
        if (ObjectUtils.isEmpty(req.getId()) || req.getId()==0){
            //新增,使用雪花算法生成id
//            doc.setId(String.valueOf(snowFlake.nextId()));
            doc.setId(snowFlake.nextId());
            System.out.println("新增数据的id"+doc.getId());
            docMapper.insert(doc);
            //新增文档内容
            content.setId(doc.getId());
            contentMapper.insert(content);
        }else {
            System.out.println("1111111111"+req.getId());
            docMapper.updateByPrimaryKey(doc);
            int count = contentMapper.updateByPrimaryKeyWithBLOBs(content);
            if (count == 0) {
                contentMapper.insert(content);
            }
        }
    }

    /**
    * @description: 删除电子书
    * @author
    * @date
    * @version 1.0
    */
    public void delete(Long id) {
        docMapper.deleteByPrimaryKey(id);
        System.out.println("删除成功！");
    }

    public void delete(List<String> ids) {
        DocExample docExample = new DocExample();
        DocExample.Criteria criteria = docExample.createCriteria();
        criteria.andIdIn(ids);
        docMapper.deleteByExample(docExample);
        System.out.println("删除成功！");
    }

    public String findContent(Long id) {
        Content content=contentMapper.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(content)) {
            return "";
        } else {
            return content.getContent();
        }
    }
}
